package com.skyvin.nreceipts.admin.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyvin.nreceipts.admin.mongorepository.RetailerRepository;
import com.skyvin.nreceipts.admin.mongorepository.RetailerTemplateRepository;
import com.skyvin.nreceipts.common.document.AppUser;
import com.skyvin.nreceipts.common.document.Retailer;
import com.skyvin.nreceipts.common.document.RetailerTemplate;
import com.skyvin.nreceipts.common.service.BaseService;
import com.skyvin.nreceipts.user.service.UserService;

@Service
public class RetailerService extends BaseService {

	Logger logger = Logger.getLogger(RetailerService.class);

	@Autowired
	RetailerTemplateRepository retailerTemplateRepository;
	
	@Autowired 
	MongoTemplate mongoTemplate;

	@Autowired
	RetailerRepository retailerRepository;	
	
	@Autowired
	UserService userService;

	@Transactional
	public void clonefromRetailerTemplate(AppUser appUser) {
		List<RetailerTemplate> retailerTemplates = retailerTemplateRepository.findAll();
		for (RetailerTemplate retailerTemplate : retailerTemplates) {
			Retailer retailer = new Retailer();
			retailer.setId(null);
			retailer.setAddress1(retailerTemplate.getAddress1());
			retailer.setAddress2(retailerTemplate.getAddress2());
			retailer.setCity(retailerTemplate.getCity());
			retailer.setCountry(retailerTemplate.getCountry());
			retailer.setEmail(retailerTemplate.getEmail());
			retailer.setPhone(retailerTemplate.getPhone());
			retailer.setValue(retailerTemplate.getValue());
			retailer.setZipcode(retailerTemplate.getZipcode());
			retailer.setUserPID(appUser.getId());
			
			retailerRepository.save(retailer);
			mongoTemplate.save(retailer, getCollection(appUser.getId(), Retailer.class));
		}
	}

	@Transactional
	public Retailer save(Retailer retailer) {
		Retailer retailerCreated = retailerRepository.save(retailer);
		mongoTemplate.save(retailer, getMyCollection(Retailer.class));
		Retailer myRetailerCreated = mongoTemplate.findOne(new Query(Criteria.where("userPID").is(retailer.getUserPID()).and("value").is(retailer.getValue())), Retailer.class, getMyCollection(Retailer.class));
		logger.info("ID Generated : " + myRetailerCreated.getId());
		return myRetailerCreated;
	}
	
	public Retailer getMyRetailerByName(String value) {
		Retailer retailer = null;	
		Query query = new Query(Criteria.where("value").is(value));
		retailer = mongoTemplate.findOne(query, Retailer.class, getMyCollection(Retailer.class));
		return retailer; 
	}
	
	public List<Retailer> searchMyRetailers(String value) {
		Query query = new Query(Criteria.where("value").regex("(?i)" + value));
		List<Retailer> retailers = mongoTemplate.find(query, Retailer.class, getMyCollection(Retailer.class));
		return retailers;
	}
	// TODO: Fix this
	public List<Retailer> getAllMyRetailers() {
		List<Retailer> retailers = mongoTemplate.findAll(Retailer.class, getMyCollection(Retailer.class));
		return retailers;
	}
	
	public List<Retailer> getAllMyRetailersForPageable(Pageable pageable) {
		List<Retailer> retailers = mongoTemplate.findAll(Retailer.class, getMyCollection(Retailer.class));
		return retailers;
	}

	// TODO: Fix this
	public void delete(String retailerId) {
		// retailerRepository.delete(retailerId);
		Query query = new Query(Criteria.where("id").is(retailerId));
		mongoTemplate.findAndRemove(query, Retailer.class, getMyCollection(Retailer.class));
	}

	public Retailer getRetailer(String retailerId) {
		Query query = new Query(Criteria.where("id").is(retailerId));
		return mongoTemplate.findOne(query, Retailer.class, getMyCollection(Retailer.class));
	}
}
